//
//  UITextField-Addition.m
//  MClient
//
//  Created by  史正烨 on 10-9-6.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCControl.h"

@implementation MALabel
- (void)drawRect:(CGRect)rect {
	[self.text drawInRect:self.bounds withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
}

@end

@implementation MCTextField
@synthesize tagEx = _tagEx;
@synthesize fieldName = _fieldName;
@synthesize formulaStyle = _formulaStyle;
@end

@implementation MCTextView
@synthesize tagEx = _tagEx;
@synthesize fieldName = _fieldName;
@synthesize formulaStyle = _formulaStyle;
@end

@implementation MCControl
@synthesize style = _controlStyle;
@synthesize control = _control;
@synthesize lableStyle = _lableStyle;
//@synthesize fieldType = _fieldType;
//@synthesize fieldName = _fieldName;


- (NSString*)text {
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).text;
	}
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).text;
	}
	
	return nil;
}
- (void)setText:(NSString*)text {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).text = [NSString stringWithFormat:@"%@", text];
	}
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).text = [NSString stringWithFormat:@"%@", text];
	}
}

- (NSString*)fieldName {
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).fieldName;
	}
	
	return nil;
}
- (void)setFieldName:(NSString*)text {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).fieldName = text;
	}
}

-(NSInteger)numberOfLines
{
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).numberOfLines;
	}
	
	return 0;
}

-(void)setNumberOfLines:(NSInteger)lines
{
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).numberOfLines = lines;
	}
}

-(NSInteger)tagEx
{
	return _tagEx;
}

-(void)setTagEx:(NSInteger)tagEx
{
	_tagEx = tagEx;
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).tagEx = tagEx;
	}
}

-(UITextAlignment)textAlignment
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).textAlignment;
	}
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).textAlignment;
	}
	
	return UITextAlignmentLeft;
}

- (void)setTextAlignment:(UITextAlignment)textAlignment {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).textAlignment = textAlignment;
	}
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).textAlignment = textAlignment;
	}
}

-(UIColor*)textColor
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).textColor;
	}
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).textColor;
	}
	
	return nil;
}

- (void)setTextColor:(UIColor*)textColor {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).textColor = textColor;
	}
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).textColor = textColor;
	}
}

-(UIColor*)backgroundColor
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).backgroundColor;
	}
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).backgroundColor;
	}
	
	return nil;
}

- (void)setBackgroundColor:(UIColor*)backgroundColor {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).backgroundColor = backgroundColor;
	}
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).backgroundColor = backgroundColor;
	}
}

-(UIFont*)font
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).font;
	}
	if (_controlStyle == MCControl_Lable) {
		return ((UILabel*)_control).font;
	}
	
	return nil;
}

- (void)setFont:(UIFont*)font {
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).font = font;
	}
	if (_controlStyle == MCControl_Lable) {
		((UILabel*)_control).font = font;
	}
}

-(UIKeyboardType) keyboardType
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).keyboardType;
	}
	
	return UIKeyboardTypeDefault;
}

-(void)setKeyboardType:(UIKeyboardType) keyboardType
{
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).keyboardType = keyboardType;
	}
}

-(BOOL)secureTextEntry
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).secureTextEntry;
	}
	
	return NO;
}

-(void)setSecureTextEntry:(BOOL)secureTextEntry
{
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).secureTextEntry = secureTextEntry;
	}
}

-(BOOL)enabled
{
	if (_controlStyle == MCControl_TextField) {
		return ((MCTextField*)_control).enabled;
	}
	
	return NO;
}

-(void)setEnabled:(BOOL)enabled
{
	if (_controlStyle == MCControl_TextField) {
		((MCTextField*)_control).enabled = enabled;
	}
}

-(void)setFrame:(CGRect)frame
{
	super.frame = frame;
	
	if (_controlStyle == MCControl_TextField && _control != nil) {
		((MCTextField*)_control).frame = self.bounds;
	}
	if (_controlStyle == MCControl_Lable && _control != nil) {
		((UILabel*)_control).frame = self.bounds;
	}
}

- (void)dealloc {
	[_fieldName release];
	[_control release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame withStyle:(MCControlStyle)style
{
	if (self = [super initWithFrame:frame]) {
		_controlStyle = style;
		if (style == MCControl_TextField) {
			_control = [[MCTextField alloc] initWithFrame:self.bounds];
			//[_control addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
		}
		if (style == MCControl_Lable) {
			_control = [[MALabel alloc] initWithFrame:self.bounds];
		}
		[self addSubview:_control];
	}
	
	return self;
}

- (void)setAutoresizingMask:(UIViewAutoresizing)autoresizing {
	super.autoresizingMask = autoresizing;
	if (_controlStyle == MCControl_TextField && _control != nil) {
		((MCTextField*)_control).autoresizingMask = autoresizing;
	}
	if (_controlStyle == MCControl_Lable && _control != nil) {
		((UILabel*)_control).autoresizingMask = autoresizing;
	}
}

- (void)textFieldDone:(id)sender
{
	[sender resignFirstResponder];
}

- (int) fieldType {
	if (_controlStyle == MCControl_TextField && _control != nil) {
		return ((MCTextField*)_control).fieldType;
	}
	
	return -1;
}

- (void) setFieldType:(int)type {
	if (_controlStyle == MCControl_TextField && _control != nil) {
		((MCTextField*)_control).fieldType = type;
	}
}
@end
